package com.example.ai.repository.impl;

import com.example.ai.repository.ChatHistoryRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.*;
@Primary
@Component
@RequiredArgsConstructor
public class RedisChatHistoryRepository implements ChatHistoryRepository {

    private final RedisTemplate<String, String> redisTemplate;

    // Redis 中的 key 前缀，避免与其他数据冲突
    private static final String CHAT_HISTORY_KEY_PREFIX = "chat:history:";

    @Override
    public void save(String type, String chatId) {
        // 使用 Redis Hash，type 作为 Hash 的 key，chatId 作为 field
        String redisKey = CHAT_HISTORY_KEY_PREFIX + type;
        redisTemplate.opsForSet().add(redisKey, chatId); // 使用 Set 确保 chatId 不重复
    }

    @Override
    public List<String> getChatIds(String type) {
        String redisKey = CHAT_HISTORY_KEY_PREFIX + type;
        Set<String> chatIds = redisTemplate.opsForSet().members(redisKey);
        return chatIds == null ? List.of() : new ArrayList<>(chatIds);
    }
}
